package com.mer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mer.dao.PermissionDao;
import com.mer.dao.RoleDao;
import com.mer.dao.RolePermissionDao;
import com.mer.dao.UserRoleDao;
import com.mer.entity.Permission;
import com.mer.entity.Role;
import com.mer.entity.RolePermission;
import com.mer.entity.UserRole;
import com.mer.service.RoleService;
import com.mer.vo.CommonResult;
import com.mer.vo.RolePermissionVo;
import com.mer.vo.RoleVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @program: IIPhase-project
 * @description:
 * @author: 冯彦凯asdf
 * @create: 2021-10-19 10:47
 **/
@Service
public class RoleServiceImpl implements RoleService {
    @Resource
    private PermissionDao permissionDao;
    @Resource
    private RoleDao roleDao;
    @Resource
    private UserRoleDao userRoleDao;
    @Resource
    private RolePermissionDao rolePermissionDao;
    @Override
    public CommonResult getAllRoleUser(Integer page, Integer limit, Role roleVo) {
        QueryWrapper<Role> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(roleVo.getRoleName())){
            wrapper.like("role_name",roleVo.getRoleName());
        }
        if (roleVo.getIsDeleted()!=null){
            wrapper.eq("is_deleted",roleVo.getIsDeleted());
        }
        Page<Role> rolePage = new Page<>(page,limit);
        Page<Role> roleData = roleDao.selectPage(rolePage, wrapper);
        //System.out.println(roleData);
        return new CommonResult(2000,"查询成功",roleData);
    }

    @Override
    public CommonResult updateRoleStatusById(String id,Integer deleted) {
        QueryWrapper<Role> wrapper = new QueryWrapper<>();
        wrapper.eq("id",id);
        List<Role> roles = roleDao.selectList(wrapper);
        for (Role role:roles){
            if (role.getId().equals("1")){
                return new CommonResult(5000,"超级管理员无法删除",null);
            }
        }
        Role role = new Role();
        role.setId(id);
        role.setIsDeleted(deleted);
        roleDao.updateById(role);
        QueryWrapper<UserRole> wrapperRole = new QueryWrapper<>();
        wrapperRole.eq("role_id",id);
        UserRole userRole = new UserRole();
        userRole.setIsDeleted(deleted);
        userRoleDao.update(userRole,wrapperRole);
        return new CommonResult(2000,"操作成功",null);
    }

    @Override
    public CommonResult addOrUpdateInfo(Role role) {
        if (StringUtils.isNotEmpty(role.getId())){
            int i = roleDao.updateById(role);
            return new CommonResult(2000,"信息修改成功",null);
        }
        role.setRoleCode("1");
        int insert = roleDao.insert(role);
        return new CommonResult(2000,"添加成功",null);
    }

    @Override
    public CommonResult deleteInfoById(String roleid) {
        roleDao.deleteById(roleid);
        /*rolePermissionDao.deleteById(roleid);
        userRoleDao.deleteById(roleid);*/
        return new CommonResult(2000,"信息删除成功",null);
    }

    @Override
    public CommonResult findPermissionByRoleId(String roleId) {
        QueryWrapper<RolePermission> wrapper = new QueryWrapper<>();
        List<Permission> Permissions = permissionDao.selectList(null);

        List<Permission> childPermission = new ArrayList<>();
        for (Permission permission : Permissions) {
            if (permission.getPid().equals("1")) {
                childPermission.add(permission);
            }
        }

        for (Permission permission : childPermission) {
            permission.setChildren(getChildPermission(Permissions, permission.getId()));
        }

        wrapper.eq("role_id", roleId);
        List<RolePermission> rolePermissions = rolePermissionDao.selectList(wrapper);
        List<String> list = rolePermissions.stream().map(i -> i.getPermissionId()).collect(Collectors.toList());
        HashMap<String, Object> mer = new HashMap<>();
        mer.put("allPermissions",childPermission);
        mer.put("checkItems",list);
        return new CommonResult(2000,"查询成功",mer);
    }

    @Override
    public CommonResult confirmRolePower(RolePermissionVo rolePermissionVo) {
        QueryWrapper<RolePermission> wrapper = new QueryWrapper<>();
        wrapper.eq("role_id",rolePermissionVo.getRoleId());
        rolePermissionDao.delete(wrapper);


        for (String permissionId:rolePermissionVo.getPermissionIds()){
            RolePermission rolePermission = new RolePermission();
            rolePermission.setIsDeleted(0);
            rolePermission.setRoleId(rolePermissionVo.getRoleId());
            rolePermission.setPermissionId(permissionId);
            rolePermissionDao.insert(rolePermission);
        }
        return new CommonResult(2000,"操作成功",null);
    }

    public List<Permission> getChildPermission(List<Permission> permission,String pid){
        List<Permission> secondPermission = new ArrayList<>();

        for (Permission permission1:permission){
            if (permission1.getPid().equals(pid)){
                secondPermission.add(permission1);
            }
        }
        for (Permission permission1:secondPermission){
            permission1.setChildren(getChildPermission(permission, permission1.getId()));
        }
        return secondPermission;
    }
}
